/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.aacorps.javaeal.service.code;

import edu.aacorps.javaeal.common.CodeDTO;
import edu.aacorps.javaeal.domain.Code;
import edu.aacorps.javaeal.remote.CodeServiceRemote;
import edu.aacorps.javaeal.repository.code.CodeRepository;
import edu.aacorps.javaeal.repository.user.UserRepository;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Ati
 */
@Stateless
public class CodeServiceBean implements CodeServiceRemote {

    @EJB
    private UserRepository userRepository;

    @EJB
    private CodeRepository codeRepository;
    
    @Override
    public Boolean registerCode(CodeDTO codeDTO) {
        if (userRepository.find(codeDTO.getUserName()) == null) {
            return null;
        }   

        Code code = codeRepository.find(codeDTO.getCode());
        if (code == null) {
            return null;
        } else {
            if (code.getUser() != null) {
                return false;
            }
        }

        codeRepository.create(codeRepository.toCodeEntity(codeDTO));

        return true;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
